﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommandLine;
using CommandLine.Text;
using Newtonsoft.Json;

namespace IPIdee.MapServices.Client.CmdUtil
{
    class Program
    {
        public static readonly HeadingInfo _headingInfo = new HeadingInfo("IPIdee.MapServices.Client.CmdUtil", "v0.1");

        static void Main(string[] args)
        {
            var options = new Options();
            ICommandLineParser parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
            
            if (!parser.ParseArguments(args, options))
            {
                Environment.Exit(1);
            }

            DoCoreTask(options);
        }

        private static void DoCoreTask(Options options)
        {
            try
            {
                if (options.Action.ToLower().Equals("resolve"))
                {
                    string fullLocationName;
                    if (!options.LocationName.Contains("^"))
                    {
                        fullLocationName = options.LocationName + "^example.com";
                    }
                    else
                    {
                        fullLocationName = options.LocationName;
                    }

                    string extendedLocationName = options.LocationName + "^example.com";
                    string locationName = extendedLocationName.Split("^".ToCharArray())[0];
                    string domainName = extendedLocationName.Split("^".ToCharArray())[1];

                    var coordinate = new IPIdee.MapServices.Client.ServiceLogic.LocationNameServiceReference.LocationNameServiceClient().ResolveToLatLngCoordinate(domainName, locationName);

                    if (coordinate == null)
                    {
                        //
                        // not found
                        //
                        Console.WriteLine(string.Format("No lookup found for this name"));
                    }
                    else
                    {
                        if (options.Output.Equals("txt"))
                        {
                            Console.WriteLine(string.Format("latitude:{0} longitude:{1} zoom:{2}", coordinate.Latitude, coordinate.Longitude, coordinate.Zoom));
                        }
                        else if (options.Output.Equals("json"))
                        {
                            string output = JsonConvert.SerializeObject(coordinate);
                            Console.WriteLine(output);
                        }
                    }
                }
                else if (options.Action.ToLower().Equals("register"))
                {
                    string fullLocationName;
                    if (!options.LocationName.Contains("^"))
                    {
                        fullLocationName = options.LocationName + "^example.com";
                    }
                    else
                    {
                        fullLocationName = options.LocationName;
                    }

                    string extendedLocationName = options.LocationName + "^example.com";
                    string locationName = extendedLocationName.Split("^".ToCharArray())[0];
                    string domainName = extendedLocationName.Split("^".ToCharArray())[1];
                    string privateKey = options.PrivateKey;

                    IPIdee.MapServices.Client.Entities.Coordinates.LatLngCoordinate coordinate = new IPIdee.MapServices.Client.Entities.Coordinates.LatLngCoordinate()
                    {
                        Latitude = options.Latitude,
                        Longitude = options.Longitude,
                        Zoom = options.ZoomLevel
                    };

                    new IPIdee.MapServices.Client.ServiceLogic.LocationNaming.LocationNameService().RegisterLocationName(privateKey, domainName, locationName, coordinate);

                    if (options.Output.Equals("txt"))
                    {
                        Console.WriteLine(string.Format("latitude:{0} longitude:{1} zoom:{2}", coordinate.Latitude, coordinate.Longitude, coordinate.Zoom));
                    }
                    else if (options.Output.Equals("json"))
                    {
                        string output = JsonConvert.SerializeObject(coordinate);
                        Console.WriteLine(output);
                    }
                    else
                    {
                        Console.WriteLine("Unsupported output type");
                    }
                }
                else
                {
                    Console.WriteLine("Unsupported action");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
